<?php
/***************************************************************************
 *   copyright : (C) 2009 Udrea Cristian
 *   site : http://code.google.com/p/testauction-php
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as 
 *   published by the Free Software Foundation; either version 2 of the
 *   License, or (at your option) any later version. Although none of the
 *   code may be sold. If you have been sold this script, get a refund.
 ***************************************************************************/
/**
 * User has submitted form without errors and user's
 * account has been edited successfully.
 */
$template->setCurrentBlock("table_page");

if(isset($_SESSION['useredit'])){
   unset($_SESSION['useredit']);
   $template->setVariable(TABLEPAGECONTENT, "Datele au fost salvate, ".$session->username.".");
}else{
/**
 * If user is not logged in, then send him back to the 
 * last page. If user is logged in, then display the 
 * form to edit account information.
 */
	if($session->logged_in){
		require("HTML/QuickForm.php");
		$userkey = $session->userinfo['userkey'];
		$result = execute_query('SELECT * FROM '.TBL_USERDATA.' WHERE userkey = ?', array($userkey));
		$DBarray = $result[0];

		$userdataform = new HTML_QuickForm('userdata', 'post', 'process.php');
		$userdataform->removeAttribute('name');
		$userdataform->addElement('static', 'email_label', 'Email', $session->userinfo['email']);
		$userdataform->addElement('text', 'surname', 'Nume', array('maxlength'=>50, 'value'=>$DBarray['surname']));
		$userdataform->addElement('text', 'gname', 'Prenume', array('maxlength'=>50, 'value'=>$DBarray['name']));
		$userdataform->addElement('text', 'telno', 'Telefon', array('maxlength'=>12, 'value'=>$DBarray['telno']));
		$userdataform->addElement('static', 'uname_label', 'Nume de utilizator', $session->username);
		$userdataform->addElement('text', 'city', 'Localitate', array('maxlength'=>32, 'value'=>$DBarray['city']));
		$userdataform->addElement('text', 'state', 'Judet', array('maxlength'=>32, 'value'=>$DBarray['state']));
		$userdataform->addElement('static', 'warn_label', 'ATENTIE', 
			'<b style="color: #F00;">Adresa va fi folosita pentru livrarea produselor <br /> castigate si TREBUIE 
			sa se regaseasca pe teritoriul ROMANIEI.</b>');
		$userdataform->addElement('text', 'postcode', 'Cod postal', array('maxlength'=>8, 'value'=>$DBarray['postcode']));
		$userdataform->addElement('textarea', 'address', 'Adresa', array('rows'=>3, 'cols'=>40))->setValue($DBarray['address']);
		$userdataform->addElement('checkbox', 'newsletter', 'Doresc sa primesc newsletter ', '')->setChecked($session->userinfo['newsletter']);
		$userdataform->addElement('hidden', 'subedit', 1);
		$userdataform->addElement('hidden', 'page', 23);
		$userdataform->addElement('submit', null, 'Salveaza', array('class'=>'lime-button'));

		$pwdchangeform = new HTML_QuickForm('pwdchange', 'post', 'process.php');
		$pwdchangeform->removeAttribute('name');
		$pwdchangeform->addElement('password', 'curpass', 'Vechea parola', array('maxlength'=>30));
		$pwdchangeform->addElement('password', 'newpass', 'Noua parola', array('maxlength'=>30));
		$pwdchangeform->addElement('password', 'repass', 'Repeta parola', array('maxlength'=>30));
		$pwdchangeform->addElement('hidden', 'subedit', 2);
		$pwdchangeform->addElement('hidden', 'page', 23);
		$pwdchangeform->addElement('submit', null, 'Trimite', array('class'=>'lime-button'));
		
		$pwdformtable = "<table cellpadding=\"0\" cellspacing=\"0\" style=\"line-height:10px\">"
			."<tr><td rowspan=\"4\">"
			.$pwdchangeform->toHtml()."</td>"
			."<td height=\"10\">".$form->error("curpass")."</td></tr>"
			."<tr><td height=\"10\">".$form->error("newpass")."</td></tr>"
			."<tr><td height=\"10\">".$form->error("repass")."</td></tr>"
			."<tr><td height=\"12\">&nbsp;</td></tr></table>";
		
		$tabctrl = new TabControl("usertabs");
		$tabctrl->addStaticTab('udata_tab','Datele Mele', (($form->num_errors > 0 && $form->value("subedit") == 1) ? "<b style=\"color: red;\">
			Nu ati completat toate campurile necesare.</b>" : "").$userdataform->toHtml());
		$tabctrl->addStaticTab('chpass_tab','Schimba Parola', (($form->num_errors > 0 && $form->value("subedit") == 2) ? "<b style=\"color: red;\">
			Nu ati completat toate campurile necesare.</b>" : "").$pwdformtable);
		$tabctrl->addAjaxTab('auction_tab', 'Licitatii Castigate', 'content.php?content=1');
		$tabctrl->addAjaxTab('offer_tab', 'Oferte', 'content.php?content=2');
		$tabctrl->addAjaxTab('invite_tab', 'Invita-ti prietenii', 'content.php?content=3');
	}else{
		header('Location: '.$session->referrer);
	}

	if($form->value("subedit") > 0){
		$tabctrl->setDefaultPage($form->value("subedit"));
	}
	else{
		$tabctrl->setDefaultPage(1);
	}
	$template->setVariable(TABLEPAGECONTENT, $tabctrl->toString());
}
$template->parseCurrentBlock();

?>
